package org.example;

import com.clf.mapper.CustomerMapper;
import com.clf.model.Customer;
import com.clf.model.CustomerExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args)throws Exception {
        System.out.println("Hello World!");

        String resource = "mybatisResources/mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class);


//        Customer customer = customerMapper.selectById4Own(2);
//        System.out.println(customer);
        System.out.println("---------------------------------------------");

        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andNameLike("%张%");
        Customer customer1 = new Customer();
        customer1.setGender("女汉子");

        //==>  Preparing: update t_customer SET gender = ? WHERE ( name like ? )
        //==> Parameters: 女汉子(String), %张%(String)
        int updateCount = customerMapper.updateByExampleSelective(customer1, customerExample);

        sqlSession.commit();

        System.out.println("updateCount = " + updateCount);;

    }
}
